

//
//  JMTableVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/4/21.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMTableVController.h"
#import "JMCellView.h"
#import "JMForgotPwdTVController.h"
#import "JMRegisterTVController.h"
#import "JMHomeVController.h"
#import "JMLoginReturn.h"



@interface JMTableVController ()<UITextFieldDelegate>

@end

@implementation JMTableVController
{
    UITextField* _textfield;
    UITextField* _passWord;
    UIButton* _rememberBtn;
    UIButton* _forgetBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"登录";
    [self setupOther];//创建tableView
    [self createButton];

}

-(void)setupOther
{
    self.tableView.scrollEnabled = NO;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 10)];
    self.tableView.tableHeaderView = view;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BACKCOLOR;
    UITapGestureRecognizer *singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1;
    [self.tableView addGestureRecognizer:singleRecognizer];
    
}
- (void)handleSingleTapFrom
{
    [_passWord resignFirstResponder];
    [_textfield resignFirstResponder];
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"LoginCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    CGFloat imageH = 50;
    UIImageView *lineImg = [[UIImageView alloc]initWithFrame:CGRectMake(0,50, 320, 1)];
    lineImg.image = [UIImage imageNamed:@"halvingline_2"];
    [cell.contentView addSubview:lineImg];
    lineImg.frame = [ToolViewAndData MyAutoLayout:lineImg];
  
    if (indexPath.row == 0) {
        UIImageView *userImage = [ARC createImageViewWithFrame:CGRectMake(10, 0, imageH, imageH) ImageName:@"aut_ico_username"];
        userImage.frame = [ToolViewAndData MyAutoLayout:userImage];
        [cell.contentView addSubview:userImage];
        
        if (!_textfield) {
            _textfield = [self addTextFieldWithFrame:CGRectMake(60, 0, 260, 50)];

            [cell.contentView  addSubview:_textfield];
        }
        
        return [JMCellView setupCell:cell HideLine:YES];
    }else if(indexPath.row == 1){
        
        UIButton *forgetBtn = [ARC createButtonWithFrame:CGRectMake(imageH*6, 5,imageH*2, imageH) Target:self Sel:@selector(forgetBtnClick:) Title:@"忘记密码?"];
        [forgetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        forgetBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        forgetBtn.frame = [ToolViewAndData MyAutoLayout:forgetBtn];
        [cell.contentView addSubview:forgetBtn];
        UIImageView *passwordImage = [ARC createImageViewWithFrame:CGRectMake(10, 0, imageH, imageH) ImageName:@"aut_ico_password2"];
        passwordImage.frame = [ToolViewAndData MyAutoLayout:passwordImage];
        [cell.contentView addSubview:passwordImage];
        
        if (!_passWord) {
         
            _passWord = [self addPwdFieldWithFrame:CGRectMake(60, 0, 180, 50)];
            [cell.contentView  addSubview:_passWord];

        }
    }
    
    return [JMCellView setupCell:cell HideLine:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CELLHIGHT;
}


- (JMTextField *)addTextFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:16
                                                       Placeholder:@"用户名，手机号码"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.keyboardType =  UIKeyboardTypePhonePad;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
     return  textField;
    
}

- (JMTextField *)addPwdFieldWithFrame:(CGRect)frame
{
    //密码
    JMTextField  *passWord= [JMTextField createTextFieldWithFrame:frame
                                                             Font:16
                                                      Placeholder:@"登录密码"
                                                  SecureTextEntry:YES
                                                        TextColor:[UIColor blackColor]];
    
    passWord.frame = [ToolViewAndData MyAutoLayout:passWord];
    
    passWord.delegate = self;
    return  passWord;
}


#pragma mark - 创建textField

#pragma mark - 创建登陆注册按钮
- (void)createButton{
    
    CGFloat btnX = 140;
    CGFloat btnH = 40;

    
    UIButton* loginBtn = [ARC createButtonWithFrame:CGRectMake(30,btnX, 260, btnH)
                                             Target:self
                                                Sel:@selector(login)
                                              Title:@"登 录"];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"logoin_btn"] forState:UIControlStateNormal];
    loginBtn.frame = [ToolViewAndData MyAutoLayout:loginBtn];
    [loginBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    loginBtn.tintColor=[UIColor whiteColor];
    [self.view addSubview:loginBtn];
    
    UIButton* registbtn = [ARC createButtonWithFrame:CGRectMake(30,btnX+50, 260, btnH)
                                              Target:self
                                                 Sel:@selector(registeClick)
                                               Title:@"注 册"];
     [registbtn setBackgroundImage:[UIImage imageNamed:@"aut_ico_register"] forState:UIControlStateNormal];
    [registbtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    registbtn.frame = [ToolViewAndData MyAutoLayout:registbtn];
    registbtn.tintColor=[UIColor redColor];

    [self.view addSubview:registbtn];
    
    
}

-(void)forgetBtnClick:(UIButton*)forgetBtn
{
    JMForgotPwdTVController *forgotVC = [[JMForgotPwdTVController alloc]init];
    [self.navigationController pushViewController:forgotVC animated:YES];
    
}
#pragma mark - 注册
-(void)registeClick
{
    JMRegisterTVController *registerVC = [[JMRegisterTVController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark - 登录
/**登陆*/
- (void)login{
//    NSDictionary *dict = [JMData dictWithPhone:_textfield.text Password:_passWord.text];
    _textfield.text = @"13333333330";
    _passWord.text = @"123456";
    if (!_textfield.text&&![ToolViewAndData isMobileNumber:_textfield.text]) {
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }

//    if ([_passWord.text isEqualToString:ZDFristPwd]) {
//        [MBProgressHUD showError:@"请输入密码"];
//        return;
//    }
    
    if (_textfield.text.length * _passWord.text.length == 0) {
        [MBProgressHUD showError:@"用户名或密码为空"];
        return;
    }

    [self TelNo:_textfield.text Pwd:_passWord.text TelCode:@"" ExtenderKey:@""];
    

}
- (void)TelNo:(NSString *)phone Pwd:(NSString *)pwd TelCode:(NSString *)telcode ExtenderKey:(NSString *)extenderKey
{
    
    NSString *urlRe = [NSString stringWithFormat:@"%@",JMURLLogin];
    NSDictionary *dictRe = [JMData TelNo:phone Pwd:pwd TelCode:telcode ExtenderKey:extenderKey];
    
    NSString *strAry = [JMData stringWithDict:dictRe];
    NSArray *aryRe = @[strAry,urlRe];
    [Request startRequestWithArray:aryRe FinishedBlock:^(NSData *data) {
        NSDictionary *dictR = [JMData dictAndTestData:data];
            JMLoginReturn *loginReturn = [JMLoginReturn objectWithKeyValues:dictR];
        [UserEntity shardUserInfo].access_token = loginReturn.data;
        if ([loginReturn.State intValue]>0) {
            JMHomeVController *homeVc = [[JMHomeVController alloc]init];
            [self.navigationController pushViewController:homeVc animated:YES];
            
        }else{
            SHOWALERT(dictR[@"Msg"]);
            
            return ;
        }
        
        
        
        
        
        
    } FailBlock:^(NSString *string) {
        [MBProgressHUD showError:@"登录失败，请梢后重试"];
    }];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:_passWord]) {
        [self login];
        return YES;
    }
    [textField resignFirstResponder];
    return YES;
    
}


#pragma mark - 禁止屏幕旋转
SHOULDAutorotate(YES)


@end
